package io.renren.modules.lucky.service.impl;

import io.renren.common.exception.RRException;
import io.renren.modules.app.utils.JwtUtils;
import io.renren.modules.lucky.dao.AppGoodsCommentDao;
import io.renren.modules.lucky.entity.AppGoodsCommentEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.lucky.dao.AppCommentLikeDao;
import io.renren.modules.lucky.entity.AppCommentLikeEntity;
import io.renren.modules.lucky.service.AppCommentLikeService;
import org.springframework.transaction.annotation.Transactional;


@Service("appCommentLikeService")
public class AppCommentLikeServiceImpl extends ServiceImpl<AppCommentLikeDao, AppCommentLikeEntity> implements AppCommentLikeService {

    @Autowired
    private JwtUtils jwtUtils;

    @Autowired
    private AppGoodsCommentDao commentDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AppCommentLikeEntity> page = this.page(
                new Query<AppCommentLikeEntity>().getPage(params),
                new QueryWrapper<AppCommentLikeEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional(readOnly = false)
    public int addOrRemoveCommentLike(String userToken, String userId, String commentId, String flag) {

        String id = jwtUtils.getClaimByToken(userToken).getSubject();

        if (userId.equals(id)) {

            QueryWrapper query = new QueryWrapper<AppCommentLikeEntity>();
            query.eq("user_id", userId);
            query.eq("comment_id", commentId);

            List<AppCommentLikeEntity> list = baseMapper.selectList(query);
            if (list.size() > 0) {

                AppCommentLikeEntity like = list.get(0);
                like.setLikeFlag(Integer.valueOf(flag));

                return baseMapper.updateById(like);

            } else {

                //查评论信息
                AppGoodsCommentEntity comment = commentDao.selectById(commentId);
                if (comment == null) {
                    throw new RRException("参数错误，此条信息不存在~");
                }

                //新增记录
                AppCommentLikeEntity like = new AppCommentLikeEntity();
                like.setLikeFlag(1);
                like.setCommentId(Integer.valueOf(commentId));
                like.setCreateTime(new Date());
                like.setUpdateTime(new Date());
                like.setUserId(Integer.valueOf(userId));
                like.setGoodId(comment.getGoodsId());

                return baseMapper.insert(like);

            }

        } else {
            throw new RRException("请求用户id与token不匹配");
        }

    }

    @Override
    public int getLikeSum(String commentId) {
        if (StringUtils.isNotEmpty(commentId)) {

            QueryWrapper query = new QueryWrapper<AppCommentLikeEntity>();
            query.eq("like_flag", 1);
            query.eq("comment_id", commentId);
            return baseMapper.selectCount(query);
        }

        return 0;
    }

}